home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewRae-c / NewRae.c next >
Encoding:
Text File  |  1994-12-04  |  7.8 KB  |  341 lines  |  [TEXT/MMCC]

  1. //•-----------------------------------------------------------------•//
  2. //• Another old C source, brought back from the dead ---------------•//
  3. //• by Kenneth A. Long, at itty bitty bytes™ -----------------------•//
  4. //• Made to run on Think C™ on 7 April 1994 ------------------------•//
  5. //• Made to run on Code Warrior on 20 November 1994 ----------------•//
  6. //• kenlong@netcom.com ---------------------------------------------•//
  7. //•-----------------------------------------------------------------•//
  8.  
  9. //o Written 11:22 am Aug 22, 1986 
  10. //o by sdh@joevax.UUCP 
  11. //o Posted to uiucdcsb:net.sources.mac.
  12. //o "source to rae demo"
  13.  
  14. //o rae.c
  15. //o This is the demo "rae" originally written for blit terminals
  16. //o converted to the Macintosh in Aztec C version 1.06H
  17. //o Steve Hawley 8/22/86
  18.  
  19. //o I can't vouch for the code for rae. It was copied directly.
  20. //o I only changed the system commands and initialization sequence.
  21.  
  22. #include <quickdraw.h>
  23. #include <events.h>
  24. #include <osutils.h>
  25.  
  26. #define HEIGHT 16
  27. #define WIDTH  16
  28. #define XMIN  0
  29. #define YMIN  0
  30. #define XMAX  qd.screenBits.bounds.right
  31. #define YMAX  qd.screenBits.bounds.bottom
  32. #define TOP   YMIN
  33. #define BOT   (YMAX-1)
  34.  
  35. short   ground[128];
  36. short   X_pos;
  37.  
  38. GrafPort myPort;
  39.  
  40. //o This is the bits for the face that gets bounced around.
  41. //o the original rae used just circles.
  42.  
  43. static unsigned char facebits[32] = {
  44.     0x07, 0xe0, 0x18, 0x18, 0x20, 0x04, 0x42, 0x42, 0x42, 0x42,
  45.     0x82, 0x41, 0x80, 0x01, 0x80, 0x01, 0x84, 0x21, 0x88, 0x11,
  46.     0x94, 0x29, 0x43, 0xc2, 0x40, 0x02, 0x20, 0x04, 0x18, 0x18,
  47.     0x07, 0xe0
  48. };
  49.  
  50. WindowPtr    raeWindow;        //• Added by KAL.
  51. Rect        windowBounds;        //• Added by KAL.
  52.  
  53. struct BitMap face;
  54.  
  55. //• MBar show/hide stuff.
  56. RgnHandle        mBarRgn, GrayRgn;
  57. short            *mBarHeightPtr;
  58. short            oldMBarHeight;
  59.  
  60.  
  61. //•-----------------------------------------------------------------•//
  62. //• Prototypes: ----------------------------------------------------•//
  63. //•-----------------------------------------------------------------•//
  64. void Hide_Menu_Bar (void);
  65. void Show_Menu_Bar (void);
  66. void Setup_Window (void);
  67. void Draw_It (short x, short y);
  68. short Friction_Decay (short v);
  69. void Frame_It (void);
  70. void main (void);
  71.  
  72. //•-----------------------------------------------------------------•//
  73. void Hide_Menu_Bar (void) 
  74. {
  75.     Rect    mBarRect;
  76.  
  77.     GrayRgn = GetGrayRgn ();
  78.     mBarHeightPtr = (short *) 0x0BAA;
  79.     oldMBarHeight = *mBarHeightPtr;
  80.     *mBarHeightPtr = 0;
  81.     mBarRect = qd.screenBits.bounds;
  82.     mBarRect.bottom = mBarRect.top + oldMBarHeight;
  83.     mBarRgn = NewRgn ();
  84.     RectRgn (mBarRgn, &mBarRect);
  85.     UnionRgn (GrayRgn, mBarRgn, GrayRgn);
  86.     PaintOne (0L, mBarRgn);
  87. }
  88.  
  89. //•-----------------------------------------------------------------•//
  90. void Show_Menu_Bar (void) 
  91. {
  92.     *mBarHeightPtr = oldMBarHeight;
  93.     DiffRgn (GrayRgn, mBarRgn, GrayRgn);
  94.     DisposeRgn (mBarRgn);
  95. }
  96.  
  97. //•-----------------------------------------------------------------•//
  98. void Setup_Window (void)
  99. {
  100.     SetRect (&windowBounds, 0, 0, XMAX, YMAX);
  101.                                 
  102.     raeWindow = NewWindow (0L, &windowBounds, "\p", true, noGrowDocProc, (WindowPtr) -1L, true, 0);
  103.     FillRect (&windowBounds, &qd.black);
  104.     SetPort (raeWindow);
  105. }
  106.  
  107. //•-----------------------------------------------------------------•//
  108. void Draw_It (short x, short y)
  109. {    
  110.     short i;
  111.     Rect trect, myRect;
  112.  
  113.     if (x < XMIN)
  114.         x = XMIN + 8;
  115.     
  116.     //• TOP is 0, so if vert. is less then it's 8 down.
  117.     if (y < TOP)
  118.         y = TOP + 8;
  119.     
  120.     //• XMAX is screen.right, so if it's more then it's 8 left.
  121.     if (x > XMAX)
  122.         x = XMAX - 8;
  123.         
  124.     X_pos = x >> 3;
  125.     if (y > ground[X_pos])
  126.         y = ground[X_pos];
  127.         
  128.     SetRect (&trect, x - 12, y - 8, x + 4, y + 8);
  129.     CopyBits (&face, &myPort.portBits, &face.bounds, &trect, srcXor, 0L);
  130. }
  131.  
  132. //•-----------------------------------------------------------------•//
  133. short Friction_Decay (short v)
  134. {
  135.     if (v == 0)
  136.         return (v);
  137.     if (v > 0)
  138.         return (v - 1 - (v >> 3));
  139.         
  140.     return (v + 1 - (v >> 3));
  141. }
  142.  
  143. //•-----------------------------------------------------------------•//
  144. void Frame_It ()
  145. {
  146.     PenPat (&qd.white);
  147.     MoveTo (XMIN, YMIN); 
  148.     LineTo (XMIN, YMAX -1);
  149.     MoveTo (XMIN, YMAX -1); 
  150.     LineTo (XMAX -1, YMAX -1);
  151.     MoveTo (XMAX -1, YMAX -1);
  152.     LineTo (XMAX -1, YMIN);
  153.     PenPat (&qd.black);
  154. }
  155.  
  156. //•-----------------------------------------------------------------•//
  157. void main ()
  158. {
  159.     Rect myRect, trect;
  160.  
  161.     register i;
  162.     register time;
  163.     short x, xx, y, yy, xvel, yvel, xacc, yacc, ypos, offset;
  164.     long waste;
  165.     WindowPtr aWindow;
  166.  
  167.     InitGraf (&qd.thePort);
  168.     InitWindows ();
  169.     
  170.     Hide_Menu_Bar ();
  171.     
  172.     Setup_Window ();
  173.     
  174.     OpenPort (&myPort); //o create my own port so I don't need.
  175.              //o windows and can blacken the whole screen.
  176.     
  177.     HideCursor ();
  178.     
  179.     face.baseAddr = (Ptr) &facebits[0]; //o set up bitmap parameters.
  180.     face.rowBytes = 2;
  181.     SetRect (&face.bounds, 0, 0, 16, 16);
  182.     
  183.     Frame_It ();
  184.     
  185.     for (i = 0; i < 128; i++) 
  186.     {
  187.         x = i << 3;
  188.         if (x <= XMIN + 8 || x >= XMAX - 8)
  189.             ground[i] = 0;
  190.         else 
  191.         {
  192.             ground[i] = BOT - 9;
  193.             if (i & 01)
  194.                 if (ground[i - 1] == 0)
  195.                     ground[i] = 0;
  196.                 else
  197.                     ground[i] -= 7;
  198.         }
  199.     }
  200.     //• Keep a tickin' while we ain't a clickin'!
  201.     while (! Button ())
  202.     {
  203.         X_pos = Random () & 0177;
  204.         if (ground[X_pos] <= TOP) 
  205.         {
  206.             Delay (1L, &waste); 
  207.             continue;
  208.         }
  209.         x = X_pos << 3;
  210.         xvel = 4 - ((Random () | 01) & 07);
  211.         yacc = 1;
  212.         yvel = 0;
  213.         y = TOP;
  214.         for (time = 0;; time++) 
  215.         {
  216.             if (Button ()) //o Was a read key command.
  217.                 return;
  218.                 
  219.             Draw_It (x, y);
  220.             xx = x; 
  221.             yy = y;     //o Save x and y.
  222.             yvel += yacc;
  223.             y += yvel;
  224.             x += xvel;
  225.             
  226.             //o Bounce?
  227.             if (y > ground[x >> 3]) 
  228.             {
  229.                 if (yvel > 5) 
  230.                 {
  231.                     Draw_It (xx, yy);
  232.                     Draw_It (x, y);
  233.                     Delay  (1L, &waste);
  234.                     Draw_It (x, y);
  235.                     Draw_It (xx, yy);
  236.                 }
  237.                 //o Side collision?
  238.                 if (y <= ground[xx >> 3]) 
  239.                 {
  240.                     x = xx;
  241.                     xvel = -xvel;
  242.                 } 
  243.                 else 
  244.                     //o Bottom?                
  245.                     if (yy <= ground[x >> 3]) 
  246.                     {
  247.                         y = yy;
  248.                         yvel = -yvel;
  249.                     }
  250.                     else 
  251.                         {    //o Corner.
  252.                             x = xx;
  253.                             y = yy;
  254.                             xvel = -xvel;
  255.                             yvel = -yvel;
  256.                     }
  257.                     if ((time & 017) == 0)
  258.                         xvel = Friction_Decay (xvel);
  259.                         
  260.                     if (xvel == 0) 
  261.                     {
  262.                         X_pos = x >> 3;
  263.                         if (ground[X_pos - 1]    <
  264.                             ground[X_pos]        &&
  265.                             ground[X_pos]         <
  266.                             ground[X_pos + 1])
  267.                             xvel = 1;
  268.                         //o Roll left.
  269.                         else 
  270.                             if (ground[X_pos - 1]    > 
  271.                                 ground[X_pos]        &&
  272.                                 ground[X_pos]         > 
  273.                                 ground[X_pos + 1])
  274.                                 xvel = -1;
  275.                             //o on hilltop.
  276.                             else 
  277.                                 if (ground[X_pos - 1]     > 
  278.                                     ground[X_pos]        &&
  279.                                     ground[X_pos]         < 
  280.                                     ground[X_pos + 1]) 
  281.                                 {
  282.                                     if (Random () & 01)
  283.                                         xvel = 1;
  284.                                     else
  285.                                         xvel = -1;
  286.                                 }
  287.                     }    //o if xvel equals zero.
  288.                     yvel = Friction_Decay (yvel);
  289.                 }
  290.             Delay (1L, &waste);
  291.             Draw_It (xx, yy);
  292.             if (xvel == 0 && yvel == 0 && y > ground[x >> 3]-4)
  293.                 break;
  294.         }    //o for time increments.
  295.         
  296.         for (;;) 
  297.         {
  298.             //o find stable position.
  299.             if (ground[X_pos - 1] < ground[X_pos] && ground[X_pos] > ground[X_pos + 1]) 
  300.             {
  301.                 Draw_It (X_pos << 3, ground[X_pos]);
  302.                 ground[X_pos] -= 21;
  303.                 if (ground[X_pos - 1] <= 0)
  304.                     ground[X_pos] -= 7;
  305.                     ground[X_pos + 1] -= 7;
  306.                 break;
  307.             }
  308.             //o roll right.
  309.             if (ground[X_pos - 1]<ground[X_pos] && ground[X_pos]<ground[X_pos+1]) 
  310.             {
  311.                 X_pos++;
  312.                 continue;
  313.             }
  314.             //o roll left.
  315.             if (ground[X_pos - 1] > ground[X_pos] && ground[X_pos] > ground[X_pos + 1]) 
  316.             {
  317.                 X_pos--;
  318.                 continue;
  319.             }
  320.             //o on hilltop, choose at Random.
  321.             if (ground[X_pos - 1] > ground[X_pos] && ground[X_pos] < ground[X_pos + 1]) 
  322.             {
  323.                 if (Random () & 01)
  324.                     X_pos++;
  325.                 else
  326.                     X_pos--;
  327.                 continue;
  328.             }
  329.             //o else botch.
  330.             Draw_It (X_pos << 3, ground[X_pos]);
  331.             PaintRect (&myRect); //o was a call to f_rect ().
  332.             break;
  333.         }    //o for ever.
  334.     }    //• while not button.
  335.     Show_Menu_Bar ();
  336. }
  337.  
  338. //•-----------------------------------------------------------------•//
  339. //• Show's over, folks!
  340. //•-----------------------------------------------------------------•//
  341.